﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TerhelesEloszto.Common;
using TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class ShowTimeTable : System.Web.UI.Page
	{
		private static IEngine engine = Engine.Instance();
		String visitorName;
		List<ITask> taskList;
		DropDownList[] ddArray;
		Label MessageLabel;

		protected void Page_Load( object sender, EventArgs e )
		{
			if( Request.QueryString["msg"] != null )
			{
				if( Request.QueryString["msg"].Equals( "1" ) ) MessageLabel.Text = "A műveletet sikeresen végrehajtottuk!";
				else if( Request.QueryString["msg"].Equals( "0" ) ) MessageLabel.Text = "A módosítás nem engedélyezett, mert ütközést okozna.";
				MessageLabel.ForeColor = System.Drawing.Color.Red;
			}
		}

		protected override void OnInit( EventArgs e )
		{
			base.OnInit( e );

			try
			{

				visitorName = User.Identity.Name;
				if( !engine.IsAdmin( visitorName ) )
				{
					if( engine.IsTeacher( visitorName ) )
					{
						Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
					}
					else
					{
						Response.Redirect( "~/Problem.aspx" );
					}

				}
				else
				{
					if( engine.StatusOfAssignment != AssignmentStatus.ok )
					{
						Response.Redirect( "~/Admin/Timetable.aspx" );
					}

					taskList = engine.GetTasks( true ).Where( t => t.RelatedTeacher != null ).ToList();
					ddArray = new DropDownList[taskList.Count];

					MessageLabel = new Label();

					Table Table1 = new Table();
					Table1.BorderStyle = BorderStyle.Groove;
					Table1.GridLines = GridLines.Both;
					Table1.CellPadding = 15;

					int colNbr = 3;
					TableRow headerRow = new TableRow();
					for( int i = 0; i < colNbr; i++ )
					{
						TableCell tCellHeader = new TableCell();
						headerRow.Cells.Add( tCellHeader );
					}
					headerRow.CssClass = "bold";
					Table1.Rows.Add( headerRow );

					headerRow.Cells[0].Text = "Feladat";
					headerRow.Cells[1].Text = "Időpont";
					headerRow.Cells[2].Text = "Oktató";

					for( int k = 0; k < taskList.Count; k++ )
					{
						TableRow row = new TableRow();
						for( int i = 0; i < colNbr; i++ )
						{
							TableCell cell = new TableCell();
							row.Cells.Add( cell );
						}

						row.Cells[0].Text = taskList[k].Name.Trim() + " (" + taskList[k].RelatedFact.Name.Trim() + ") ";


						int j = 0;
						if( taskList[k].Type.HasInterval )
						{
							foreach( IInterval actInt in taskList[k].GetTimeIntervals() )
							{
								String day = "";
								switch( actInt.Day )
								{
									case DayOfWeek.Monday: day = "H"; break;
									case DayOfWeek.Tuesday: day = "K"; break;
									case DayOfWeek.Wednesday: day = "Sze"; break;
									case DayOfWeek.Thursday: day = "Cs"; break;
									case DayOfWeek.Friday: day = "P"; break;
									case DayOfWeek.Saturday: day = "Szo"; break;
									case DayOfWeek.Sunday: day = "V"; break;
								}

								String partity = actInt.Parity ? "Páratlan" : "Páros";
								if( j > 0 ) row.Cells[1].Text = row.Cells[1].Text + "<br/>";
								row.Cells[1].Text = row.Cells[1].Text + partity + " " + day + " " + actInt.BeginHour + "-" + actInt.EndHour;
								j++;
							}
						}
						else
						{
							row.Cells[1].Text = "Heti " + taskList[k].Hours + " óra.";
						}

						ddArray[k] = new DropDownList();
						int index = -1;
						for( int l = 0; l < taskList[k].PossibleTeachers.Count; l++ )
						{
							ITeacher t = taskList[k].PossibleTeachers[l];
							string s = "";
							s += t.Name.Trim() + " - " + t.Neptun.Trim() + " ";

							int star = t.GetStars( taskList[k] );
							string starString = "";
							for( int m = 0; m < star; m++ )
							{
								starString += "+";
							}
							if( star == 0 ) starString = "semmiképp";
							if( star == 6 ) starString = "mindenképp";
							s += "(" + starString + ")";
							ddArray[k].Items.Add( s );
							if( taskList[k].RelatedTeacher == t ) index = l;
						}
						ddArray[k].SelectedIndex = index;
						row.Cells[2].Controls.Add( ddArray[k] );
						Table1.Rows.Add( row );
					}

					Table1.Style["position"] = "inherited";
					Table1.Style["Top"] = "20px";
					ContentPlaceHolder c = (ContentPlaceHolder)( Page.Form.FindControl( "MainContent" ) );
					c.Controls.Add( MessageLabel );

					Panel p0 = new Panel();
					p0.Height = 20;
					p0.Width = 100;
					c.Controls.Add( p0 );

					c.Controls.Add( Table1 );

					Panel p = new Panel();
					p.Height = 50;
					p.Width = 100;
					c.Controls.Add( p );

					Button b = new Button();
					b.Style["position"] = "inherited";
					b.Style["Top"] = "20px";
					b.Text = "Mentés";
					b.Click += new EventHandler( b_Click );
					c.Controls.Add( b );
				}
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Admin/ShowTimetable.aspx?msg=0" );
			}
		}

		void b_Click( object sender, EventArgs e )
		{
			try
			{
				for( int k = 0; k < taskList.Count; k++ )
				{
					taskList[k].RelatedTeacher = taskList[k].PossibleTeachers[ddArray[k].SelectedIndex];

				}

				engine.UpdateAssignment( taskList );
				Response.Redirect( "~/Admin/ShowTimetable.aspx?msg=1" );
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Admin/ShowTimetable.aspx?msg=0" );
			}
		}
	}
}